
/**
 * tree
 * @type {{view: {selectedMulti: boolean, expandSpeed: string}, data: {simpleData: {enable: boolean, idKey: string, pIdKey: string, rootId: string}, key: {name: string}}, callback: {onClick: clickTree}}}
 */
var privilegeObj;
var PrivilegeTreeComponents = function (){
    var tree_obj;
    var treeNodes_obj;
    var zTree_obj;
    var treeChecked;
    var _idKey="id1";
    var _pIdKey="parentId1";
    var _name="name1";
    var _clickTree=true;
    var tree_Obj;
    var text;
    var code;
    var kindId;
    var nodes;
    var setting;
    var _url;
    var _fnClick;
    var _fnCheck;
    var zTreeOnCheck=function (e, treeId, treeNode, clickFlag) {
        debugger;
        // treeNode.tId + ", " + treeNode.name + "," + treeNode.checked
        // zTree.checkNode(treeNode, !treeNode.checked, true);
    };
    var _fn=function clickTree(event, treeId, treeNode) {
        debugger;
        // tree_Obj.checkNode(treeNode, !treeNode.checked, true);
        text = treeNode.name;
        var id=0;
        var isArea=0;
//            code = treeNode.catCode;
        if(_clickTree){
            kindId = treeNode.id;
        }else{
            kindId = treeNode.tid;
            id=treeNode.id;
            isArea=treeNode.isArea;
        }


        nodes = treeNode;
        isSelect = true;
    }

    /**
     *
     * @param id 当前节点id
     * @param pid 父节点id
     * @param name 显示名称
     */
    function init_tree(id,pid,name,fnClick,fnCheck,chkStyle){
        _idKey=id;
        _pIdKey=pid;
        _name=name;
        // _clickTree=flag;
        if(fnClick){
            _fnClick=fnClick;
        }
        if(fnCheck){
            _fnCheck=fnCheck;
        }
        var check={
            enable: false,
            // chkboxType: { "Y": "p", "N": "s" }
        };
        if(chkStyle){
            check={
                enable: true,
                chkStyle: chkStyle,
                // chkboxType: { "Y": "p", "N": "s" }
            };
        }
        setting = {
            view: {
                fontCss: getFontCss,
                selectedMulti: false,
                expandSpeed: "fast"
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: _idKey,
                    pIdKey: _pIdKey,
                    rootId: ""
                },
                key: {
                    name: _name
                }
            },
            check: check,
            callback: {
                onClick: _fnClick,
                onCheck: _fnCheck
            }
        }
    }
    function loadMyTree(url,treeid) {
        var ztreeId="kindTree";
        if(treeid){
            ztreeId=treeid;
        }
        $.ajax({
            async: false,
            url: url,
            type: "post",
            dataType: 'json',
            success: function (data) {
                treeNodes_obj = data;
                for (var key in treeNodes_obj) {
                    treeNodes_obj[key].open = true;
                }
                zTree_obj = $.fn.zTree.init($("#"+ztreeId), setting, treeNodes_obj);
                privilegeObj = $.fn.zTree.getZTreeObj(ztreeId);
                var node = privilegeObj.getNodeByParam("id", -1, null);
                privilegeObj.selectNode(node);
                //cleanAll();
                //color
                // for(var i=0;i<treeNodes_obj.length;i++){
                //     var color=""
                //     if(treeNodes_obj[i].warningType=="0"){
                //
                //     }else if(treeNodes_obj[i].warningType=="1"){
                //
                //     }else if(treeNodes_obj[i].warningType=="2"){
                //
                //     }else if(treeNodes_obj[i].warningType=="3"){
                //
                //     }else if(treeNodes_obj[i].warningType=="5"){
                //
                //     }
                //
                //     // tree_Obj.setting.view.fontCss["color"]=
                //     //
                //     // tree_Obj.updateNode(treeNodes_obj[i]);
                // }

            }
        })
    }
    function loadFenceTree(url,treeid) {
        var ztreeId="kindTree";
        if(treeid){
            ztreeId=treeid;
        }
        $.ajax({
            async: false,
            url: url,
            type: "post",
            dataType: 'json',
            success: function (data) {
                treeNodes_obj = data;
                for (var key in treeNodes_obj) {
                    treeNodes_obj[key].open = true;
                }
                zTree_obj = $.fn.zTree.init($("#kindTree"), setting, treeNodes_obj);
                privilegeObj = $.fn.zTree.getZTreeObj(ztreeId);
                var node = privilegeObj.getNodeByParam("id", -1, null);
                privilegeObj.selectNode(node);

            }
        })
    }
    function getFontCss(treeId, treeNode) {
        var cls="";
        var color="";
        if(!!treeNode.highlight){
            cls={color:"#46a647", "font-weight":"bold"};
            return cls;
        }
        if(treeNode.warningType=="0"){
            color="#333"
        }else{
            color="#1c4a9a"
        }
        if(!treeNode.isCar){
            color="#333"
        }
        return {color:color, "font-weight":"bold"};
        // return (!!treeNode.highlight) ? {color:"#5579a6", "font-weight":"bold"} : {color:"#333", "font-weight":"normal"};
    }

    /**
     * 展开树
     * @param treeId
     */
    function expand_ztree(treeId){
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        treeObj.expandAll(true);
    }

    /**
     * 收起树：只展开根节点下的一级节点
     * @param treeId
     */
    function close_ztree(treeId){
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        var nodes = treeObj.transformToArray(treeObj.getNodes());
        var nodeLength = nodes.length;
        for (var i = 0; i < nodeLength; i++) {
            if (nodes[i].id == '-1') {
                //根节点：展开
                treeObj.expandNode(nodes[i], true, true, false);
            } else {
                //非根节点：收起
                treeObj.expandNode(nodes[i], false, true, false);
            }
        }
    }

    /**
     * 搜索树，高亮显示并展示【模糊匹配搜索条件的节点s】
     * @param treeId
     * @param searchConditionId 文本框的id
     */
    function search_ztree(treeId, searchConditionId){
        searchByFlag_ztree(treeId, searchConditionId, "");
    }

    /**
     * 搜索树，高亮显示并展示【模糊匹配搜索条件的节点s】
     * @param treeId
     * @param searchConditionId     搜索条件Id
     * @param flag                  需要高亮显示的节点标识
     */
    function searchByFlag_ztree(treeId, searchConditionId, flag){
        //<1>.搜索条件
        var searchCondition = $('#' + searchConditionId).val();
        debugger;
        //<2>.得到模糊匹配搜索条件的节点数组集合
        var highlightNodes = new Array();
        if (searchCondition != "") {
            var treeObj = $.fn.zTree.getZTreeObj(treeId);
            highlightNodes = treeObj.getNodesByParamFuzzy("name", searchCondition, null);
        }
        //<3>.高亮显示并展示【指定节点s】
        highlightAndExpand_ztree(treeId, highlightNodes, flag);
    }

    /**
     * 高亮显示并展示【指定节点s】
     * @param treeId
     * @param highlightNodes 需要高亮显示的节点数组
     * @param flag           需要高亮显示的节点标识
     */
    function highlightAndExpand_ztree(treeId, highlightNodes, flag){
        debugger;
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        //<1>. 先把全部节点更新为普通样式
        var treeNodes = treeObj.transformToArray(treeObj.getNodes());
        for (var i = 0; i < treeNodes.length; i++) {
            treeNodes[i].highlight = false;
            treeObj.updateNode(treeNodes[i]);
        }
        //<2>.收起树, 只展开根节点下的一级节点
        close_ztree(treeId);

        //<3>.把指定节点的样式更新为高亮显示，并展开
        if (highlightNodes != null) {
            for (var i = 0; i < highlightNodes.length; i++) {
                if (flag != null && flag != "") {
                    if (highlightNodes[i].flag == flag) {
                        //高亮显示节点，并展开
                        highlightNodes[i].highlight = true;
                        treeObj.updateNode(highlightNodes[i]);
                        //高亮显示节点的父节点的父节点....直到根节点，并展示
                        var parentNode = highlightNodes[i].getParentNode();
                        // var parentNodes = getParentNodes_ztree(treeId, parentNode);
                        // treeObj.expandNode(parentNodes, true, false, true);
                        treeObj.expandNode(parentNode, true, false, true);
                    }
                } else {
                    //高亮显示节点，并展开
                    highlightNodes[i].highlight = true;
                    treeObj.updateNode(highlightNodes[i]);
                    //高亮显示节点的父节点的父节点....直到根节点，并展示
                    var parentNode = highlightNodes[i].getParentNode();
                    // var parentNodes = getParentNodes_ztree(treeId, parentNode);
                    // treeObj.expandNode(parentNodes, true, false, true);
                    treeObj.expandNode(parentNode, true, false, true);
                }
            }
        }
    }
    /**
     * 搜索树，高亮显示并展示【模糊匹配搜索条件的节点s】
     * @param treeId
     * @param searchConditionId 文本框的id
     */
    function search_ztree_content(treeId, key,value){
        return  searchByFlag_ztree_content(treeId, key, value,false,'');
    }

    /**
     * 搜索树，高亮显示并展示【模糊匹配搜索条件的节点s】
     * @param treeId
     * @param searchConditionId     搜索条件Id
     * @param flag                  需要高亮显示的节点标识
     */
    function searchByFlag_ztree_content(treeId, key, value,isFuzzy,flag){
        //<1>.搜索条件
        var searchCondition = value;
        debugger;
        //<2>.得到模糊匹配搜索条件的节点数组集合
        var highlightNodes = new Array();
        if (searchCondition != "") {
            var treeObj = $.fn.zTree.getZTreeObj(treeId);
            if(isFuzzy){
                highlightNodes = treeObj.getNodesByParamFuzzy(key, searchCondition, null);

            }else{
                highlightNodes = treeObj.getNodesByParam(key, searchCondition, null);

            }
        }
        return highlightNodes;
        //<3>.高亮显示并展示【指定节点s】
        // highlightAndExpand_ztree_content(treeId, highlightNodes, flag);
    }

    /**
     * 高亮显示并展示【指定节点s】
     * @param treeId
     * @param highlightNodes 需要高亮显示的节点数组
     * @param flag           需要高亮显示的节点标识
     */
    function highlightAndExpand_ztree_content(treeId){
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        //<1>. 先把全部节点更新为普通样式
        var treeNodes = treeObj.transformToArray(treeObj.getNodes());
        for (var i = 0; i < treeNodes.length; i++) {
            treeNodes[i].highlight = false;
            treeObj.updateNode(treeNodes[i]);
        }
        //<2>.收起树, 只展开根节点下的一级节点
        close_ztree(treeId);
    }
    function highlight_Expand_ztree_content(treeId){
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        //<1>. 先把全部节点更新为普通样式
        var treeNodes = treeObj.transformToArray(treeObj.getNodes());
        for (var i = 0; i < treeNodes.length; i++) {
            treeNodes[i].highlight = false;
            treeObj.updateNode(treeNodes[i]);
        }

    }

    function hightreeBynodes(treeId,highlightNodes,flag) {
        var high=true;
        if(flag){
            high=flag
        }
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        var treeNodes = treeObj.transformToArray(treeObj.getNodes());
        for (var i = 0; i < highlightNodes.length; i++) {
            highlightNodes[i].highlight = high;
            treeObj.updateNode(highlightNodes[i]);
            //高亮显示节点的父节点的父节点....直到根节点，并展示
            var parentNode = highlightNodes[i].getParentNode();
            // var parentNodes = getParentNodes_ztree(treeId, parentNode);
            // treeObj.expandNode(parentNodes, true, false, true);
            treeObj.expandNode(parentNode, true, false, true);
        }
    }
    /**
     * 递归得到指定节点的父节点的父节点....直到根节点
     */
    function getParentNodes_ztree(treeId, node){
        if (node != null) {
            var treeObj = $.fn.zTree.getZTreeObj(treeId);
            var parentNode = node.getParentNode();
            return getParentNodes_ztree(treeId, parentNode);
        } else {
            return node;
        }
    }

    /**
     * 设置树节点字体样式
     */
    function setFontCss_ztree(treeId, treeNode) {
        if (treeNode.id == 0) {
            //根节点
            return {color:"#333", "font-weight":"bold"};
        } else if (treeNode.isParent == false){
            //叶子节点
            return (!!treeNode.highlight) ? {color:"#ff0000", "font-weight":"bold"} : {color:"#660099", "font-weight":"normal"};
        } else {
            //父节点
            return (!!treeNode.highlight) ? {color:"#ff0000", "font-weight":"bold"} : {color:"#333", "font-weight":"normal"};
        }
    }

    return {
        initUserTree:function (id,pid,name,fnClick,fnCheck,chkStyle) {
            init_tree(id,pid,name,fnClick,fnCheck,chkStyle);
        },
        loadUserTree:function (url,treeid) {
            loadMyTree(url,treeid)
        }
    }
}();


